package com.darwin.darwinDll.analyze
{
	import com.adobe.serialization.json.JSON;
	import com.darwin.darwinDll.core.Config;
	import com.darwin.darwinDll.core.DllItem;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	/**
	 * Json格式文件解析器
	 * @author Roland
	 */
	public class JsonAnalyze implements IAnalyze
	{
		public function JsonAnalyze()
		{
		}
		
		public function getDataList():Dictionary
		{
			return _jsonList;
		}
		
		private var _jsonUrlList:Dictionary = new Dictionary();
		private var _jsonList:Dictionary = new Dictionary();
		public function analyze(byteArray:ByteArray, dllItem:DllItem, compFn:Function):void
		{
			if(_jsonUrlList[dllItem.url] != null)//已经解析过
			{
				dllItem.data = byteArray;
				dllItem.result = _jsonUrlList[dllItem.url];
				dllItem.status = DllItem.STATUS_LOADED;
				if(compFn != null)
				{
					compFn(dllItem);
				}
			}else{
				byteArray.position = 0;
				var json:String = byteArray.readUTFBytes(byteArray.length);
				var jsonObj:Object =  com.adobe.serialization.json.JSON.decode(json);
				dllItem.data = byteArray;
				dllItem.result = jsonObj;
				dllItem.status = DllItem.STATUS_LOADED;
				_jsonList[dllItem.name] =jsonObj;
				_jsonUrlList[dllItem.url] = jsonObj;
				if(dllItem.flag == "serverid")
				{
					Config.getInstance().registerMatSheet(dllItem.result);
				}
				if(compFn!=null)
				{
					compFn(dllItem);
				}
			}
		}
		
		/**
		 *	返回Object格式数据
		 * @param key 文件名或者serverId
		 * @param subKey 二级键值
		 * @return 
		 * 
		 */
		public function getData(key:String, subKey:String):*
		{
			var retObj:Object = _jsonList[key];
			if(retObj == null)
			{
				retObj = _jsonList[key + "$" + subKey];
			}
			return retObj;
		}
		
		/**
		 *	 根据传入url返回Object格式数据
		 * @param url 
		 * 
		 */
		public function getDataByUrl(url:String):*
		{
			var retObj:Object = _jsonUrlList[url];
			return retObj;
		}
	}
}